package errors

import (
	"fmt"
	"net/http"
)

// AppError 应用错误类型
type AppError struct {
	Code    int    `json:"code"`    // HTTP 状态码
	Message string `json:"message"` // 错误消息
	Err     error  `json:"-"`       // 原始错误
}

func (e *AppError) Error() string {
	if e.Err != nil {
		return fmt.Sprintf("%s: %v", e.Message, e.Err)
	}
	return e.Message
}

// NewAppError 创建新的应用错误
func NewAppError(code int, message string, err error) *AppError {
	return &AppError{
		Code:    code,
		Message: message,
		Err:     err,
	}
}

// 预定义错误
var (
	ErrInvalidRequest    = NewAppError(http.StatusBadRequest, "Invalid request parameters", nil)
	ErrUnauthorized      = NewAppError(http.StatusUnauthorized, "Unauthorized", nil)
	ErrForbidden         = NewAppError(http.StatusForbidden, "Forbidden", nil)
	ErrNotFound          = NewAppError(http.StatusNotFound, "Resource not found", nil)
	ErrInternalServer    = NewAppError(http.StatusInternalServerError, "Internal server error", nil)
	ErrDatabaseOperation = NewAppError(http.StatusInternalServerError, "Database operation failed", nil)
	ErrUserNotFound      = NewAppError(http.StatusNotFound, "User not found", nil)
	ErrPaymentNotFound   = NewAppError(http.StatusNotFound, "Payment not found", nil)
	ErrDuplicateUsername = NewAppError(http.StatusBadRequest, "Username already exists", nil)
	ErrDuplicateEmail    = NewAppError(http.StatusBadRequest, "Email already exists", nil)
	ErrInvalidPassword   = NewAppError(http.StatusUnauthorized, "Invalid password", nil)
)
